//
//  MarketViewCell.m
//  TableBarControllerDemo
//
//  Created by 边果互动 on 2021/3/16.
//

#import "MarketViewCell.h"
#import "MarketItemCell.h"
#import "GroupBean.h"
#import <SDWebImage/SDWebImage.h>

#define cell_id_marketItem @"MarketItemCell"

@interface MarketViewCell() <UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;

@end

@implementation MarketViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.totalCountTv.textColor = HomeCount_Color;
    self.onlineCountTv.textColor = HomeCount_Color;
    
    [self.collectionView registerNib:[UINib nibWithNibName:cell_id_marketItem bundle:nil] forCellWithReuseIdentifier:cell_id_marketItem];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - DataSource
//返回多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//返回每组多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.groups.count;
}

//返回每一个item的cell
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GroupBean *bean = self.groups[indexPath.row];
    MarketItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cell_id_marketItem forIndexPath:indexPath];
    [cell.iconIv sd_setImageWithURL:[NSURL URLWithString:bean.avatar]];
    cell.nameTv.text = bean.name;
    return cell;
}

//返回每个单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = self.bottomView.frame.size.width / 4;
    CGFloat height = self.bottomView.frame.size.height / 2.5;
    return CGSizeMake(width, height);
}
#pragma mark -

- (UIView *)collectionView {
    if (nil == _collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //设置每一行的最小间隔（因为是水平滑动，所以一行就是竖着的）
        flowLayout.minimumLineSpacing = 1.0f;
//        flowLayout.minimumInteritemSpacing = 1.0f;
//        flowLayout.sectionInset = UIEdgeInsetsMake(5.0f, 5.0f, 5.0f, 5.0f);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.bottomView.frame.size.height) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.automaticallyAdjustsScrollIndicatorInsets = NO;
        //设置是否分页滑动，（一页一页的滑）
        _collectionView.pagingEnabled = YES;
        [self.bottomView addSubview:_collectionView];
    }
    return _collectionView;
}

- (void)setGroups:(NSMutableArray *)groups {
    _groups = groups;
    [self.collectionView reloadData];
}

@end
